package com.wn.class30.cotroller.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.wn.class30.entity.Myorder;
import com.wn.class30.service.OrderService;
import com.wn.class30.service.WXMyOrderService;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import com.wn.class30.tools.WuLiuTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping(value = "/weixin")
@RestController
@Api(value = "订单功能",tags ="订单功能")
@Validated
public class MyOrderController {
    @Resource
    private WXMyOrderService wxMyOrderService;
    @Resource
    private OrderService orderService;
    @Resource
    private UseridUtils useridUtils;
    @GetMapping(value = "/queryOrder")
    @ApiOperation(value = "查询所有订单",notes = "查询所有订单")
    public Result queryOrder(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid!=null){
        List<Myorder> myorders = orderService.queryOrderByUserId(userid);
       /* System.out.println(myorders.get(0).getMyorderDetails().size());*/
        return new Result("200","success",null,myorders);
        }
        return new Result("200","success",null,null);
    }
    @RequestMapping(value="/queryMyOrderByOrderId")
    public Result queryMyOrderByOrderId(@Valid @NotNull Integer myorderId) throws Exception {

        Myorder myorder = orderService.queryOrderByOrderId(myorderId);
        String wuliuNumber = myorder.getWuliuNumber();
        String wuliuGongshi = myorder.getWuliuGongshi();
        String wuLiuMessage = WuLiuTools.getWuLiuMessage(wuliuNumber, wuliuGongshi);
        return new Result("200","success",wuLiuMessage,null);

    }
        @RequestMapping(value = "/shouhuo")
    public Result shouhuo(@Valid @NotNull Integer myorderid) throws Exception {
        System.out.println(myorderid);
            Myorder myorder = orderService.queryOrderByOrderId(myorderid);
            String wuliuNumber = myorder.getWuliuNumber();
            String wuliuGongshi = myorder.getWuliuGongshi();
            String wuLiuMessage = WuLiuTools.getWuLiuMessage(wuliuNumber, wuliuGongshi);
            JSONObject jsonObject = JSON.parseObject(wuLiuMessage);
            JSONObject result = (JSONObject) jsonObject.get("result");
            if("3".equals(result.get("deliverystatus"))){
                /* 0：快递收件(揽件)1.在途中 2.正在派件 3.已签收 4.派送失败 5.疑难件 6.退件签收  */
                wxMyOrderService.updateShouhuo(myorderid);
                return new Result("200","success","success",null);
            }else{
                return new Result("200","success",null,null);
            }
        }
}
